<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addfn"
              >新增角色</el-button>
            </el-row>
            <el-dialog
              :title="isEdit ? '编辑' : '添加'"
              :visible="showDialog"
              :close-on-press-escape="true"
              :close-on-click-modal="false"
              @close="btnCancel"
            >
              <el-form
                ref="roleForm"
                :model="roleForm"
                :rules="rules"
                label-width="100px"
              >
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="8">
                  <el-button size="small" @click="btnCancel">取消</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hSubmit"
                  >确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
            <!-- 表格 -->
            <el-table v-if="abc" :data="roleList">
              <el-table-column
                label="序号"
                width="120"
                align="center"
                type="index"
              />
              <el-table-column
                label="角色名称"
                width="240"
                align="center"
                prop="name"
              />
              <el-table-column label="描述" align="center" prop="description" />
              <el-table-column label="操作" align="center" width="240">
                <template slot-scope="scope">
                  <!--  slot-scope="scope" -->
                  <el-button size="small" type="success" @click="authority(scope.row)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="doEdit(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="deletee(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="currentchange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>

    </div>
    <!-- 分配权限的弹层 -->
    <el-dialog
      :title="tit"
      :visible.sync="showAssignDialog"
      @close="close"
    >
      <permission ref="setlist" :curid="curid" @close="close" /></el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  deleteRole,
  addRole,
  updateRole
} from '../../api/setting'
import permission from './comopen/assignPermission'
export default {
  components: {
    permission
  },
  data() {
    return {
      tit: '',
      curid: null, // 当前点击分配权限保留的id
      showAssignDialog: false, // 点击分配权限显示
      abc: true,
      isEdit: false, // 是否是编辑或添加
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      },
      roleList: [], // 承接数组
      page: {
        page: 1,
        pagesize: 5, // 几条的数据
        total: 5
      }
    }
  },

  created() {
    console.log(1)
    this.getRoleList() // 获取角色列表

    // this.doEdit()
  },
  methods: {
    // 关闭当前弹窗
    close() {
      this.showAssignDialog = false
    },
    // 点击分配权限
    authority(row) {
      this.tit = row.name + '权限'
      this.curid = row.id
      this.showAssignDialog = true
      this.$nextTick(() => {
        this.$refs.setlist.setlist()
      })
    },
    async doEdit({ id, name, description }) {
      this.showDialog = true
      this.roleForm = { id, name, description }
      // console.log(this.roleForm)
      // console.log(this.roleForm)
      await updateRole(this.roleForm)
      // console.log(re)

      this.isEdit = true

      // this.hCancel()
      // console.log(9)
    },

    addfn() {
      this.showDialog = true
      this.isEdit = false
    },
    btnCancel() {
      console.log(12333333333333333)
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
      this.getRoleList()
    },
    hSubmit() {
      this.$refs.roleForm.validate((validate) => {
        if (validate) {
          if (this.isEdit) {
            console.log(1)
            this.doEdit(this.roleForm)
            console.log(2)
            this.$message.success('操作成功')
            this.showDialog = false
            this.getRoleList()
          } else {
            // console.log(2)
            addRole(this.roleForm).then((res) => {
              // console.log(res)
              // console.log(3)
              this.getRoleList()
              // console.log(4)
              this.showDialog = false
              // 重新拉取数据
            })
          }
        }
      })
    },
    async getRoleList() {
      const res = await getRoleList(this.page) // 获取数据
      // console.log(res)
      this.roleList = res.data.rows // 铺数据
      this.page.total = res.data.total
    },
    deletee(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          console.log('????')
          // 调用删除接口
          deleteRole(id).then((res) => {
            // console.log(res)
            console.log('????')
            if (this.roleList.length === 1) {
              this.page.page--
              console.log(2)
              // this.getRoleList()
              this.$message.success('删除角色成功')
              this.getRoleList()
            } else {
              this.$message.success('删除角色成功')
              this.getRoleList()
            }
          })
          // 提示用户删除成功
        })
        .catch((re) => {
          // console.log(re)
          this.getRoleList()
        })
    },
    currentchange(i) {
      // console.log();
      // console.log(this.computed.data)
      // console.log(this.page)
      console.log(i)
      this.page.page = i
      this.getRoleList(this.page)
    }
  }
}
</script>

<style scoped>
</style>
